import axios from "axios"

export interface SQLReader {
    table: string,
    keys: Array<string>,
    key_types: Array<string>,
    where: Array<string> | null,
    where_key: string | null,
    group_by_keys: string | Array<string> | null,
    order_by_keys: string | Array<string> | null,
    limit: number | null,
}

async function readSQL(readKey: SQLReader): Promise<any> {
    let ans
    if (readKey.group_by_keys instanceof Array) readKey.group_by_keys = readKey.group_by_keys.join(' ')
    if (readKey.order_by_keys instanceof Array) readKey.order_by_keys = readKey.order_by_keys.join(' ')
    await axios({
        method: 'post',
        url: '/api/sql-select',
        data: readKey,
    }).then((res) => {
        // console.log(res)
        ans = res
    }).catch((err) => {
        console.error(err)
    })
    return ans
}

export default {
    readSQL,
}